// 定义ScoreBoard积分类
export default class ScoreBoard {
    // 设置score分数,level等级属性
    private _score = 0;
    private _level = 1;
    // 设置等级限制
    private _maxLevel: number;
    // 设置等级变化条件（多少分升一级）
    private _upScore: number;
    // 设置分数,等级的元素节点（在构造函数中初始化）
    scoreEle: HTMLElement;
    levelEle: HTMLElement;
    // maxLevel: number = 10  表示参数maxLevel传参以传递的参数为准，不传参默认值为10
    constructor(maxLevel: number = 10, upScore: number = 10) {
        this._maxLevel = maxLevel;
        this._upScore = upScore;
        this.scoreEle = document.getElementById("score")!;
        this.levelEle = document.getElementById("level")!;

    }

    // 设置私有属性的get/set
    get score(): number {
        return this._score;
    }

    set score(value: number) {
        this._score = value;
    }

    get level(): number {
        return this._level;
    }

    set level(value: number) {
        this._level = value;
    }

    get maxLevel(): number {
        return this._maxLevel;
    }

    set maxLevel(value: number) {
        this._maxLevel = value;
    }

    get upScore(): number {
        return this._upScore;
    }

    set upScore(value: number) {
        this._upScore = value;
    }

    // 设置加分的方法
    addScore() {
        /* this._score++;
        // 通过拼串转换为字符串格式
        this.scoreEle.innerHTML = this._score + ""; */
        // 简写
        this.scoreEle.innerHTML = ++this._score + "";

        // 判断分数达到条件升级
        if (this._score % this._upScore === 0) { // 20分升一级
            this.levelUp();
        }
    }

    // 提升等级的方法
    private levelUp() {
        if (this._level < this._maxLevel) {
            this.levelEle.innerHTML = ++this._level + '';
        }

    }
}


/* // 测试ScoreBoard代码
const scoreBoard = new ScoreBoard();
for (let n = 0; n < 50; n++) {
    scoreBoard.addScore();
} */